import requests
import urllib
import sys
import os
import re
'''
url="http://www.contactmusic.com/pics/ld/active_for_life_arrivals_090110/a.j_buckley_2706152.jpg"
filePath="/home/sun/dataset/vgg_face_dataset/helo.jpg"
urllib.urlretrieve(url,filePath)
'''

import socket
socket.setdefaulttimeout(500000)



def cbk(a, b, c):  
    per = 100.0 * a * b / c  
    if per > 100:  
        per = 100  
    print '%.2f%%' % per

'''
file_list=os.listdir("./files")

for file in file_list:
	dir_name="./images/%s"%file[:-4]
	if os.path.exists( dir_name )==False:
		os.mkdir( dir_name )

	f=open( os.path.join( "files",file ) )
	text=f.read()
	url_list=re.findall("(http.*?) ",text)
	num_list=re.findall("(\d{8}) http",text )
	print "we are in %s"%dir_name
	for index,url in enumerate(url_list):
		if os.path.exists( "%s/%s.jpg"%(dir_name,num_list[index]) ):
			continue
		else:
			print "downloading %s/%s.jpg"%(dir_name,num_list[index]) 
			print url
			urllib.urlretrieve(url,"%s/%s.jpg"%(dir_name,num_list[index]),cbk)
		#print url
		#os.system("touch %s/%s.jpg"%(dir_name,num_list[index]) ) 
		#print "%s/%s.jpg download completed"%(dir_name,num_list[index])
	f.close()

print "finish"

'''
file_list=os.listdir("./files")

for file in file_list:
	dir_name="./urls/%s"%file[:-4]
	if os.path.exists( dir_name )==False:
		os.mkdir( dir_name )

	f=open( os.path.join( "files",file ) )
	text=f.read()
	url_list=re.findall("(http.*?) ",text)
	num_list=re.findall("(\d{8}) http",text )
	
	tmp=open("%s/url.txt"%(dir_name),"w+")
	for i in url_list:
		tmp.write(i)
		tmp.write("\n")
	tmp.close()
	f.close()

print "finish"
